package queue;

public class MyCircularQueue {
    private int[] elements;
    public int front;
    public int rear;

    private boolean flg;

    private int usedSize;

    public MyCircularQueue(int k) {
        elements = new int[k];
    }

    //入队
    public boolean enQueue(int value) {
        if(isFull()) {
           return false;
        }
        elements[rear] = value;
        rear = (rear+1)%elements.length;
        usedSize++;
//        flg = true;
        return true;
    }

    public boolean deQueue() {
        if(isEmpty()) {
            return false;
        }
        front = (front+1)%elements.length;
        usedSize--;
/*        if(front==rear) {
            flg = false;
        }*/
        return true;
    }

    public int Front() {
        if(isEmpty()) {
           return -1;
        }
        return elements[front];
    }

    public int Rear() {
        if(isEmpty()) {
            return -1;
        }
        int index = (rear==0)?(elements.length-1):(rear-1);
        return elements[index];
/*        if(rear==0) {
            return elements[elements.length-1];
        } else {
            return elements[rear-1];
        }*/
    }

    public boolean isEmpty() {
        return front==rear&&usedSize==0;
//        return front==rear&&flg==false;
    }

    public boolean isFull() {
        return front==rear&&usedSize==elements.length-1;
//        return (rear+1)%elements.length==front;
    }
}

/**
 * Your MyCircularQueue object will be instantiated and called as such:
 * MyCircularQueue obj = new MyCircularQueue(k);
 * boolean param_1 = obj.enQueue(value);
 * boolean param_2 = obj.deQueue();
 * int param_3 = obj.Front();
 * int param_4 = obj.Rear();
 * boolean param_5 = obj.isEmpty();
 * boolean param_6 = obj.isFull();
 */